﻿using AutoMapper;
using BlogSystem.Common.Config;
using BlogSystem.Common.Config.DI.annotation;
using BlogSystem.Data.Entity;
using BlogSystem.Model.Respones;

namespace BlogSystem.Data.Repository.RepositoryImpl;


[Ioc("scoped")]
public class UserRepository : BaseRepository<User>, IUserRepository
{
    private readonly DataBaseContext _dbContext;

    private readonly IMapper _mapper;

    public UserRepository(DataBaseContext databaseContext, IMapper mapper) : base(databaseContext)
    {
        _mapper = mapper;
        _dbContext = databaseContext;
    }
    
    public async Task<IQueryable<UserResponse>> GetAllByPage(string keyWord, int pageIndex, int pageSize)
    {
        var usersPage = _dbContext.Set<User>()
            .Where(a => a.Status == 1 && (a.Name.Contains(keyWord) || a.Phone.Contains(keyWord)))
            .OrderBy(b => b.CreateTime)
            .Skip((pageIndex - 1) * pageSize)
            .Take(pageSize)
            .Select(e => _mapper.Map<UserResponse>(e));
     
        return usersPage;
    }

    public async Task<List<User>> GetByName(string name)
    {
        var users = _dbContext.Set<User>().Where(a => a.Name.Equals(name)).ToList();
        return users;
    }

    public async Task<List<User>> GetByNameOrPhone(string name, string phone)
    {
        var users = _dbContext.Set<User>().Where(a => a.Name.Equals(name) || a.Phone.Equals(phone)).ToList();
        return users;
    }
}